package ctgu.junitTest;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

import org.junit.jupiter.params.provider.MethodSource;
import java.util.stream.Stream;
import org.junit.jupiter.params.provider.Arguments;
import ctgu.java.TwoIntegers;

class TwoIntegersTest {

  private final TwoIntegers adder = new TwoIntegers();

  // 无效输入测试用例
  @ParameterizedTest(name = "num1={0}, num2={1}")
  @CsvSource({
          "0, 40",     
          "40, 0",    
          "40, 60", 
  })
  void sumInvalidCases(int num1, int num2) {
    Assertions.assertThrows(
        AssertionError.class,
        () -> adder.sum(num1, num2));
  }

  //有效输入测试用例
  @ParameterizedTest(name = "num1={0}, num2={1}, result={2}")
  @CsvSource({
          "40, 40, 80",  
          "1, 40, 41", 
           "2,40,42", 
          "40, 1, 41", 
          "40, 2, 42",
          "40,59,99",
          "40,58,98"
  })
  void sumValidCases(int num1, int num2, int result) {
    Assertions.assertEquals(result, adder.sum(num1, num2));
  }

    //采用参数生成方法实现有效输入测试用例
  @ParameterizedTest(name = "num1={0}, num2={1}, result={2}")
  @MethodSource("generator")
  void sumValidCasesWithMethodParam(int num1, int num2, int result) {
    Assertions.assertEquals(result, adder.sum(num1, num2));
  }
  
// 参数生成方法必须为static
  private static Stream<Arguments> generator(){
      return Stream.of(
          Arguments.of(40,40,80),
          Arguments.of(1,40,41),
          Arguments.of(2,40,42),
          Arguments.of(40,1,41),
          Arguments.of(40,2,42),
          Arguments.of(40,59,99),
          Arguments.of(40,58,98)
      );
  }
  
//无效输入测试用例
 @ParameterizedTest(name = "num1={0}, num2={1}")
 @CsvSource({
         "60, 0",     
         "100, 60",    
         "60, 61", 
 })
 void subInvalidCases(int num1, int num2) {
   Assertions.assertThrows(
       AssertionError.class,
       () -> adder.sub(num1, num2));
 }

 //有效输入测试用例
 @ParameterizedTest(name = "num1={0}, num2={1}, result={2}")
 @CsvSource({
         "60, 20, 40",  
         "60, 1, 59", 
          "60,2,58", 
         "98, 60, 38", 
         "99, 60, 39",
         "60,60,0",
         "60,59,1"
 })
 void subValidCases(int num1, int num2, int result) {
   Assertions.assertEquals(result, adder.sub(num1, num2));
 }

   //采用参数生成方法实现有效输入测试用例
 @ParameterizedTest(name = "num1={0}, num2={1}, result={2}")
 @MethodSource("subgenerator")
 void subValidCasesWithMethodParam(int num1, int num2, int result) {
   Assertions.assertEquals(result, adder.sub(num1, num2));
 }
 
//参数生成方法必须为static
 private static Stream<Arguments> subgenerator(){
     return Stream.of(
         Arguments.of(60,20,40),
         Arguments.of(60,1,59),
         Arguments.of(60,2,58),
         Arguments.of(98,60,38),
         Arguments.of(99,60,39),
         Arguments.of(60,60,0),
         Arguments.of(60,59,1)
     );
 }
}